﻿Imports IniFile
Public Class SwitchUserForm

    Private Sub SwitchUserForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Settings.ParseSettings()
            UserNameInput.Text = Settings.UserName
            PasswordInput.Text = Settings.Password
            DefChanInput.Text = Settings.DefaultChannel
            ServerInput.Text = Settings.Server

            If Settings.AutoLogin = "0" Then
                AutoLoginInput.SelectedIndex = AutoLoginInput.Items.IndexOf("No")
            ElseIf Settings.AutoLogin = "1" Then
                AutoLoginInput.SelectedIndex = AutoLoginInput.Items.IndexOf("Yes")
            End If
        Catch ex As Exception
            Console.WriteLine("SWITCH USER FORM LOAD SWITCH FORM FUNCTION ERROR: " + ErrorToString())
        End Try
    End Sub

    Private Sub SetDefaultButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetDefaultButton.Click
        Try
            Settings.UserName = UserNameInput.Text
            Settings.Password = PasswordInput.Text
            Settings.DefaultChannel = DefChanInput.Text
            Settings.Server = ServerInput.Text
            If AutoLoginInput.SelectedIndex = 0 Then
                Settings.AutoLogin = "0"
            ElseIf AutoLoginInput.SelectedIndex = 1 Then
                Settings.AutoLogin = "1"
            End If


            If System.IO.File.Exists("settings.ini") = True Then
                System.IO.File.Delete("settings.ini")
            End If

            Settings.CreateSettings()
        Catch ex As Exception
            Console.WriteLine("SET AS DEFAULT BUTTON CLICK SWITCH FORM FUNCTION ERROR: " + ErrorToString())
        End Try
    End Sub

    Private Sub LoginBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginBut.Click
        Try
            Settings.UserName = UserNameInput.Text
            Settings.Password = PasswordInput.Text
            Settings.DefaultChannel = DefChanInput.Text
            Settings.Server = ServerInput.Text

            If BNetworking.T.Connected = False Then
                MainForm.LoginLogoutBut_Click(MainForm, EventArgs.Empty)
            Else
                BNetworking.ConnectionClosed()
                TextHandling.QueueMessage("", "Already Logged in. Disconnecting and logging in with new details.", "System")
                MainForm.LoginLogoutBut.Text = "Login"
                MainForm.LoginLogoutBut_Click(MainForm, EventArgs.Empty)
            End If

            Me.Close()
        Catch ex As Exception
            Console.WriteLine("LOGIN BUTTON SWITCH FORM FUNCTION ERROR: " + ErrorToString())
        End Try
    End Sub
End Class